/**
 * 
 */
package com.svn;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collection;
import java.util.Iterator;

import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.fs.FSRepositoryFactory;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import com.jie.eclipse.pan.dao.QMDao;

/**
 * @author yanjie
 *
 */
public class SvnUtils {
	private static ISVNAuthenticationManager authManager = null;
	private static QMDao qm = null;
	private static String ip = "";
	public static void main(String[] args) {
		qm = QMDao.getInstance();
		
		String url = "file:///b:/jmPro/jmPro/WebRoot";
		String name = "yanjie";
		String password = "yanjie";
		setupLibrary();
		SVNRepository repository = null;
		try {
			InetAddress addr=InetAddress.getLocalHost();
			ip = addr.getHostAddress();
			repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
			authManager = SVNWCUtil
					.createDefaultAuthenticationManager(name, password);
			repository.setAuthenticationManager(authManager);
	
			SVNNodeKind nodeKind = repository.checkPath("", -1);
			if (nodeKind == SVNNodeKind.NONE) {
				System.err.println("There is no entry at '" + url + "'.");
			} else if (nodeKind == SVNNodeKind.FILE) {
				System.err.println("The entry at '" + url
						+ "' is a file while a directory was expected.");
			}
			
			System.out.println("Repository Root: "+ repository.getRepositoryRoot(true));

			listEntries(repository, "");
		
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private static void setupLibrary() {
		DAVRepositoryFactory.setup();
		SVNRepositoryFactoryImpl.setup();
		FSRepositoryFactory.setup();
	}

	public static void listEntries(SVNRepository repository, String path)
			throws SVNException {
		Collection entries = repository.getDir(path, -1, null,(Collection) null);
		
		Iterator iterator = entries.iterator();
		while (iterator.hasNext()) {
			SVNDirEntry entry = (SVNDirEntry) iterator.next();
			String name = entry.getName();
			if(name.indexOf(".") < 0 && !name.equals("include") && !name.equals("report")  && !name.equals("WEB-INFO")){
				SVNRepository rep = SVNRepositoryFactory.create(SVNURL
						.parseURIEncoded("file:///b:/jmPro/jmPro/WebRoot/"+name));
				rep.setAuthenticationManager(authManager);
				recordEntries(rep,name);
			}
		}
	}
	
	public static void recordEntries(SVNRepository repository, String parentName) throws SVNException{
		Collection entries = repository.getDir("", -1, null,(Collection) null);
		Iterator iterator = entries.iterator();
		while (iterator.hasNext()) {
			SVNDirEntry entry = (SVNDirEntry) iterator.next();
			String name = entry.getName();
			if(name.endsWith(".jsp")){
				System.out.println("/jmPro/"+parentName+"/"+name+"--"+entry.getAuthor()+"--"+ip);
				qm.registerJspInfo("/jmPro/"+parentName+"/"+name, name, entry.getAuthor(), ip, false);
			}
		}
	}
}
